package com.css.zfzx.sjcj.modules.hddcwySamplePoint.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwySamplePoint.repository.HddcWySamplepointNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwySamplePoint.repository.HddcWySamplepointRepository;
import com.css.zfzx.sjcj.modules.hddcwySamplePoint.repository.entity.HddcWySamplepointEntity;
import com.css.zfzx.sjcj.modules.hddcwySamplePoint.service.HddcWySamplepointService;
import com.css.zfzx.sjcj.modules.hddcwySamplePoint.viewobjects.HddcWySamplepointQueryParams;
import com.css.zfzx.sjcj.modules.hddcwySamplePoint.viewobjects.HddcWySamplepointVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-01
 */
@Service
public class HddcWySamplepointServiceImpl implements HddcWySamplepointService {

	@Autowired
    private HddcWySamplepointRepository hddcWySamplepointRepository;
    @Autowired
    private HddcWySamplepointNativeRepository hddcWySamplepointNativeRepository;

    @Override
    public JSONObject queryHddcWySamplepoints(HddcWySamplepointQueryParams queryParams , int curPage, int pageSize) {
        Page<HddcWySamplepointEntity> hddcWySamplepointPage =
                this.hddcWySamplepointNativeRepository.queryHddcWySamplepoints(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWySamplepointPage);
        return jsonObject;
    }


    @Override
    public HddcWySamplepointEntity getHddcWySamplepoint(String id) {
        HddcWySamplepointEntity hddcWySamplepoint = this.hddcWySamplepointRepository.findById(id).orElse(null);
         return hddcWySamplepoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWySamplepointEntity saveHddcWySamplepoint(HddcWySamplepointEntity hddcWySamplepoint) {
        String uuid = UUIDGenerator.getUUID();
        hddcWySamplepoint.setId(uuid);
        hddcWySamplepoint.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWySamplepoint.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWySamplepointRepository.save(hddcWySamplepoint);
        return hddcWySamplepoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWySamplepointEntity updateHddcWySamplepoint(HddcWySamplepointEntity hddcWySamplepoint) {
        hddcWySamplepoint.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWySamplepoint.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWySamplepointRepository.save(hddcWySamplepoint);
        return hddcWySamplepoint;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWySamplepoints(List<String> ids) {
        List<HddcWySamplepointEntity> hddcWySamplepointList = this.hddcWySamplepointRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWySamplepointList) && hddcWySamplepointList.size() > 0) {
            for(HddcWySamplepointEntity hddcWySamplepoint : hddcWySamplepointList) {
                this.hddcWySamplepointRepository.delete(hddcWySamplepoint);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
